<template>
  <div class="menu-create">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>{{ isEditor ? '编辑菜单':'添加菜单' }}</span>
      </div>
      <div class="text item">
        <el-form ref="form" :model="form" label-width="80px">
          <el-form-item label="菜单名称">
            <el-input v-model="form.name"></el-input>
          </el-form-item>
          <el-form-item label="菜单路径">
            <el-input v-model="form.href"></el-input>
          </el-form-item>
          <el-form-item label="上级菜单">
            <el-select v-model="form.parentId" placeholder="请选择上级菜单">
              <el-option label="无上级菜单" :value="-1"></el-option>
              <el-option
                v-for="item in parentMenuList"
                :label="item.name"
                :value="item.id"
                :key="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="描述">
            <el-input v-model="form.description"></el-input>
          </el-form-item>
          <el-form-item label="前端图片">
            <el-input v-model="form.icon"></el-input>
          </el-form-item>
          <el-form-item label="是否显示">
            <el-radio-group v-model="form.shown">
              <el-radio :label="true">是</el-radio>
              <el-radio :label="false">否</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="排序">
            <el-input v-model="form.orderNum"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">{{ isEditor ? '提交修改' : '立即创建'}}</el-button>
            <el-button v-if="!isEditor">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script lang="ts">
import Vue from 'vue'
import { createOrUpdateMenu, getEditMenuInfo } from '@/services/menu'
export default Vue.extend({
  name: 'menuCreateOrEditor',
  props: {
    isEditor: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      form: {
        name: '1',
        href: '1',
        parentId: -1,
        description: '1',
        icon: '1',
        shown: false,
        orderNum: 0
      },
      parentMenuList: []
    }
  },
  created () {
    this.getparentId()
  },
  methods: {
    async getparentId () {
      const { data } = await getEditMenuInfo(this.$route.params.id || -1)
      if (data.data.menuInfo) {
        this.form = data.data.menuInfo
      }
      if (data.code === '000000') {
        this.parentMenuList = data.data.parentMenuList
      }
    },
    async onSubmit () {
      const { data } = await createOrUpdateMenu(this.form)
      console.log(data)
      if (data.code === '000000') {
        this.$message.success('成功~')
        this.$router.back()
      }
    }
  }
})
</script>

<style> </style>
